local Widget            = require "widgets/widget"
local Image             = require "widgets/image"
local Text              = require "widgets/text"

local TINT              = { 1, 1, 1, 1 }
local FONT              = TALKINGFONT or HEADERFONT

-- 独立的竖向体力条 Widget
local AssnStaminaWidget = Class(Widget, function(self)
    Widget._ctor(self, "AssnStaminaWidget")

    -- 容器，用于定位整条体力条
    self.bar_root = self:AddChild(Widget("StaminaBarRoot"))
    -- 默认放置在父节点中心
    self.bar_root:SetPosition(-60, 3)

    -- 背景条 (竖直方向)
    self.bar_bg = self.bar_root:AddChild(Image("images/global.xml", "square.tex"))
    self.bar_bg:SetTint(0, 0, 0, .4)
    self.bar_bg:SetSize(100, 10)

    self.bar_bg:SetVRegPoint(ANCHOR_LEFT)
    self.bar_bg:SetHRegPoint(ANCHOR_LEFT)

    -- 前景条
    self.bar_fg = self.bar_root:AddChild(Image("images/global.xml", "square.tex"))
    self.bar_fg:SetTint(0.2, 0.8, 0.9, 1)
    self.bar_fg:SetSize(100, 10)

    self.bar_fg:SetVRegPoint(ANCHOR_LEFT)
    self.bar_fg:SetHRegPoint(ANCHOR_LEFT)

    -- 数值文本
    self.text = self:AddChild(Text(FONT, 20, "mp 80/80", TINT))
    self.text:SetPosition(0, -20) -- 位于条下方
end)

--- 刷新体力条显示
-- @param current 当前体力
-- @param max     最大体力
function AssnStaminaWidget:OnUpdate(current, max)
    -- 更新文本
    self.text:SetString(string.format("mp %d/%d", current, max))
    -- 更新前景条高度
    local pct = (max > 0) and (current / max) or 0
    self.bar_fg:SetSize(100 * pct, 10) -- 宽高
end

return AssnStaminaWidget
